﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using WpfDataBingding.DevTreeData;
using bus=WpfDataBingding.TaskServiceReference;

namespace WpfDataBingding
{
    /// <summary>
    /// MonAgentDetail.xaml 的交互逻辑
    /// </summary>
    public partial class MonAgentDetail : UserControl
    {
       
        private Dictionary<string, int> MonDeviceRecord_Dictionary;//定义该结构用于保存界面选定监控的设备key=设备ID，内容=监控时隙

        /// <summary>
        /// 构造函数
        /// </summary>
        public MonAgentDetail(MonitorAgent monAgent)
        {
            InitializeComponent();
            this.DataContext = monAgent;
            MonDeviceRecord_Dictionary = new Dictionary<string, int>();
            init_MonDeviceRecord_Dictionary();
        }

        //根据数据总线发来的设备初始化界面记录
        public void init_MonDeviceRecord_Dictionary()
        {
            if (this.DataContext == null)
                throw new ApplicationException("MonAgentDetail对象数据源为空，初始化出错");
            MonitorAgent monAgent=  (MonitorAgent)this.DataContext;

            foreach (Device dev in monAgent.DeviceList)
            {
                if (dev.IsMonitered)
                {
                    MonDeviceRecord_Dictionary.Add(dev.DevID, 0);
                }
            }
        }

        /// <summary>
        /// 将DevDetail的数据源属性绑定到DevInfo_lay
        /// </summary>        
        new public Object DataContext
        {
            get { return this.devInfoList_GroupBox.DataContext; }
            set { this.devInfoList_GroupBox.DataContext = value; }
        }



        /// <summary>
        /// 根据ListView中选项结构添加和删除设备监控
        /// </summary>
        private void MonTaskChange_Button_Click(object sender, RoutedEventArgs e)
        {

            List<Device> devAddList = new List<Device>(4);
            List<Device> devDeleList = new List<Device>(4);
            List<int> timeList = new List<int>(4);
            bus.DataBusServiceClient serviceClient = new bus.DataBusServiceClient();
            try
            {
                foreach (Device dev in ((MonitorAgent)this.DataContext).DeviceList)
                {
                    if (this.MonDeviceRecord_Dictionary.ContainsKey(dev.DevID))//设备需要监控
                    {
                        if (dev.IsMonitered == false)//未被监控
                        {                             
                            int time = MonDeviceRecord_Dictionary[dev.DevID];
                            if (time <= 0)
                                throw new ApplicationException("设备"+dev.DevID+ "监控时间未正确设置");
                            //添加监控
                            devAddList.Add(dev);
                            timeList.Add(time);                            
                        }
                    }
                    else//不需要监控
                    {
                        if (dev.IsMonitered)//已经被监控
                        {   //删除监控
                            devDeleList.Add(dev);                      
                        }
                    }
                }//foreach
                //调用远程服务添加监控任务
                MonTaskConfigration.addMonDevTaskS(serviceClient,devAddList,timeList);
                //调用远程服务删除监控任务
                MonTaskConfigration.deleteMonDevTaskS(serviceClient, devDeleList);
            }
            catch (Exception exp)
            {
                MessageBox.Show("出错：" + exp.Message);
                return;
            }
            finally
            {
                if (serviceClient != null)
                    serviceClient.Close();
            }

            MessageBox.Show("监控任务修改成功");
        }


        /// <summary>
        /// CheckBox单击事件：对应修改源设备的监控状态
        /// </summary>
        private void CheckBox_Click(Object sender, RoutedEventArgs e)
        {
            WpfDataBingding.DevTreeData.Device dev_Source = (WpfDataBingding.DevTreeData.Device)(((CheckBox)sender).DataContext);
            bool curIsMonitered = dev_Source.IsMonitered;
            dev_Source.IsMonitered = curIsMonitered==true?false:true;//还原数据绑定自动修改的值

            if (curIsMonitered)//由未监控变为监控
            {
                //将选择记录到MonDeviceRecord_Dictionary,监控时隙暂时标记为0  
                this.MonDeviceRecord_Dictionary.Add(dev_Source.DevID, 0);
            }
            else//由监控变为未监控
            {
                this.MonDeviceRecord_Dictionary.Remove(dev_Source.DevID);
                dev_Source.MonTimeInterval ="";//时间清零
            }
            //if (dev_Source.IsMonitered==false && this.MonDeviceRecord_Dictionary.ContainsKey(dev_Source.DevID) == false)//记录不存在该设备则添加
            //{
            //    this.MonDeviceRecord_Dictionary.Add(dev_Source.DevID, 0); //将选择记录到MonDeviceRecord_Dictionary,监控时隙暂时标记为0  
            //}
            ////当设备正被监控，且已被选入需要监控设备的结构时--移除
            //else if (dev_Source.IsMonitered==true && this.MonDeviceRecord_Dictionary.ContainsKey(dev_Source.DevID))
            //{
            //    this.MonDeviceRecord_Dictionary.Remove(dev_Source.DevID);
            //}
            ////当设备正被监控，且未被选入需要监控设备的结构时--添加
            //else if (dev_Source.IsMonitered == true && this.MonDeviceRecord_Dictionary.ContainsKey(dev_Source.DevID) == false)
            //{
            //    this.MonDeviceRecord_Dictionary.Add(dev_Source.DevID, 0); //将选择记录到MonDeviceRecord_Dictionary,监控时隙暂时标记为0  
            //}
            //else if(dev_Source.IsMonitered==false && this.MonDeviceRecord_Dictionary.ContainsKey(dev_Source.DevID) == true)//记录不存在该设备则添加
            //{
            //    this.MonDeviceRecord_Dictionary.Remove(dev_Source.DevID);
            //}
        }

        /// <summary>
        /// timeInterval_TextBox_TextChanged事件：对应修改源设备的监控时间间隔
        /// </summary>
        private void timeInterval_TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textbox = (TextBox)sender;
            WpfDataBingding.DevTreeData.Device dev_Source = (WpfDataBingding.DevTreeData.Device)(textbox.DataContext);
            //判断对应设备未被监控则无法设置时间间隔
            if(this.MonDeviceRecord_Dictionary.ContainsKey(dev_Source.DevID)==false)
            {
                //MessageBox.Show("设定设备监控时间间隔之前，请先将该设备监控状态钩选为监控");
                textbox.Text = "";
                return;
            }
            int timeInterval=0;
            try 
	        {
                timeInterval = Convert.ToInt32(textbox.Text.Trim());
                if (timeInterval < 0)
                    throw new Exception();
                this.MonDeviceRecord_Dictionary[dev_Source.DevID] = timeInterval;
	        }
	        catch (Exception)
	        {
                MessageBox.Show("监控时间值只能为正整数");
                //textbox.Text = "";
	        }                        
        }

    }
}
